# -*- coding: utf-8 -*-
# @Date   : 2020/01/07
# @File   : IC_CardAndQuasiUsedAmtPro_ZBC.py
# @Author : yanhaopeng

'''eof
name:贷记卡授信额度使用情况
code:IC_CardAndQuasiUsedAmtPro_ZBC
tableName:
columnName:
groups:征信模块-准入策略
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
1.Factor1 = 贷记卡账户汇总信息段(PC02H)·已用额度(PC02HJ04) + 准贷记卡账户汇总信息段(PC02I)·透支余额(PC02IJ04)
2.Factor2 = 贷记卡账户汇总信息段(PC02H)·授信总额(PC02HJ01) + 准贷记卡账户汇总信息段(PC02I)·授信总额(PC02IJ01)
3.Factor = Factor1/Factor2
'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def factor1(data):
    factor = []
    #信贷交易信息概要   [1..1]
    PCO = getTable(data, 'PCO', 1)
    if PCO is None:
        return None
    elif PCO == [{}]:
        return 0
    else:
        #借贷交易信息概要信息单元   [0..1]
        PC02 = getTable(PCO[0], 'PC02', 0)
        if PC02 is None:
            return None
        elif PC02 == [{}]:
            return 0
        else:
            # 贷记卡账户汇总信息段   [0..1]
            PC02H = getTable(PC02[0], 'PC02H', 0)
            #准贷记卡账户汇总信息段   [0..1]
            PC02I = getTable(PC02[0], 'PC02I', 0)
            if PC02H is None:
                factor.append(None)
            elif PC02H == [{}]:
                factor.append(0)
            else:
                #已用额度   [1..1]
                PC02HJ04 = PC02H[0].get('PC02HJ04')
                if PC02HJ04 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02HJ04):
                        factor.append(float(PC02HJ04))
                    else:
                        factor.append(None)
            if PC02I is None:
                factor.append(None)
            elif PC02I == [{}]:
                factor.append(0)
            else:
                #透支余额   [1..1]
                PC02IJ04 = PC02I[0].get('PC02IJ04')
                if PC02IJ04 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02IJ04):
                        factor.append(float(PC02IJ04))
                    else:
                        factor.append(None)

            factor = filter(lambda x: x is not None, factor)
            if len(factor) == 0:
                return None
            else:
                return sum(factor)

def factor2(data):
    factor = []
    #信贷交易信息概要   [1..1]
    PCO = getTable(data, 'PCO', 1)
    if PCO is None:
        return None
    elif PCO == [{}]:
        return 0
    else:
        #借贷交易信息概要信息单元   [0..1]
        PC02 = getTable(PCO[0], 'PC02', 0)
        if PC02 is None:
            return None
        elif PC02 == [{}]:
            return 0
        else:
            #贷记卡账户汇总信息段   [0..1]
            PC02H = getTable(PC02[0], 'PC02H', 0)
            #准贷记卡账户汇总信息段   [0..1]
            PC02I = getTable(PC02[0], 'PC02I', 0)
            if PC02H is None:
                factor.append(None)
            elif PC02H == [{}]:
                factor.append(0)
            else:
                #授信总额   [1..1]
                PC02HJ01 = PC02H[0].get('PC02HJ01')
                if PC02HJ01 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02HJ01):
                        factor.append(float(PC02HJ01))
                    else:
                        factor.append(None)
            if PC02I is None:
                factor.append(None)
            elif PC02I == [{}]:
                factor.append(0)
            else:
                #授信总额   [1..1]
                PC02IJ01 = PC02I[0].get('PC02IJ01')
                if PC02IJ01 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02IJ01):
                        factor.append(float(PC02IJ01))
                    else:
                        factor.append(None)

            factor = filter(lambda x: x is not None, factor)
            if len(factor) == 0:
                return None
            else:
                return sum(factor)

def IC_CardAndQuasiUsedAmtPro():
    data = IC_DSJ_2D['data']
    Factor1 = factor1(data)
    Factor2 = factor2(data)

    if Factor1 is None or Factor2 is None or Factor2 == 0:
        return u'缺失值'
    else:
        return Factor1/Factor2

result = IC_CardAndQuasiUsedAmtPro()

# import json
# ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511", "creditCode": "123456",
#                  "regNumber": "12345", "relativeDate":u'2019/08/09'}
# with open('F:/hp/IC_DSJ1.json', 'r') as f:
#     IC_DSJ_2D = json.loads(f.read())['IC_DSJ']
# print IC_CardAndQuasiUsedAmtPro()